﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace ZoomExperiment
{
    public partial class FirstTest : PhoneApplicationPage
    {
        #region //----------------------------- Class Private Fields --------------------------------------//
        private double _initialScale;

        private Point _oldFinger1, _oldFinger2;
        private Point _currentFinger1, _currentFinger2;
        private Point _oldCenter, _currentCenter;

        private Point _imagePosition;
        private Size _imageSize;
        #endregion

        #region //----------------------------- Class Public Constructors ---------------------------------//
        public FirstTest()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(OnLoaded);
        }
        #endregion

        #region //----------------------------- Private Event Handlers ------------------------------------//
        private void OnLoaded(object sender, RoutedEventArgs args)
        {
            _imagePosition = new Point(0, 0);
            _imageSize = new Size(
                (image.Parent as Grid).ActualWidth,
                (image.Parent as Grid).ActualHeight);
        }

        private void OnPinchStarted(object sender, PinchStartedGestureEventArgs args)
        {
            _initialScale = transform.ScaleX;

            _oldFinger1 = args.GetPosition(image, 0);
            _oldFinger2 = args.GetPosition(image, 1);
            _oldCenter = CalculateCenter(_oldFinger1, _oldFinger2);
        }

        private void OnPinchDelta(object sender, PinchGestureEventArgs args)
        {
            _currentFinger1 = args.GetPosition(image, 0);
            _currentFinger2 = args.GetPosition(image, 1);
            _currentCenter = CalculateCenter(_currentFinger1, _currentFinger2);

            Point scaledOldCenter = new Point(
                _oldCenter.X * args.DistanceRatio, 
                _oldCenter.Y * args.DistanceRatio);

            // TODO: thay đổi vị trí của view
            double deltaX = (_oldCenter.X * (args.DistanceRatio - 1));
            double deltaY = (_oldCenter.Y * (args.DistanceRatio - 1));

            _imagePosition.X += deltaX;
            _imagePosition.Y += deltaY;

            Point screenScaledOldCenter = ConvertToScreenCoordinate(scaledOldCenter);
            Point screenCurrentCenter = ConvertToScreenCoordinate(_currentCenter);

            Point transitionDelta = new Point(
                screenCurrentCenter.X - screenScaledOldCenter.X,
                screenCurrentCenter.Y - screenScaledOldCenter.Y);

            UpdateImage(args.DistanceRatio, transitionDelta);
        }

        private void OnPinchCompleted(object sender, PinchGestureEventArgs args)
        {

        }        
        #endregion

        #region //----------------------------- Class Private Methods -------------------------------------//
        /// <summary>
        /// Update transform of image
        /// </summary>
        /// <param name="scale">distance ratio from last scale</param>
        /// <param name="delta">the delta which image will be moved</param>
        private void UpdateImage(double scale, Point delta)
        {
            transform.ScaleX = transform.ScaleY = _initialScale * scale;
            transform.TranslateX += delta.X;
            transform.TranslateY += delta.Y;

            //_imagePosition.X += delta.X;
            //_imagePosition.Y += delta.Y;
        }

        private Point CalculateCenter(Point p1, Point p2)
        {
            return new Point((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);
        }

        private Point ConvertToScreenCoordinate(Point p)
        {
            return new Point(_imagePosition.X + p.X, _imagePosition.Y + p.Y);
        }
        #endregion
    }
}